﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Cassini;

namespace TV4Home.WebInterface.StandaloneTestWebServer
{
    public partial class MainForm : Form
    {
        private Server webServer;

        public MainForm()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            string path = System.IO.Path.GetFullPath(txtPathToFiles.Text);
            int port = (int)this.nudPort.Value;
            string virtualDir = "/TV4Home";

            if (!CheckIfDefaultAspxExistsInDirectory(path))
                return;

            try
            {
                this.webServer = new Server(port, virtualDir, path);
                this.webServer.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            this.linkLabelWebserverUrl.Text = String.Format("http://{0}:{1}{2}/Default.aspx", Environment.MachineName, port, virtualDir);
            this.pnWebserverUrl.Visible = true;

            this.btnStop.Enabled = true;
            this.btnStart.Enabled = false;
            this.pnMain.Enabled = false;
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            try
            {
                this.webServer.Stop();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            this.pnWebserverUrl.Visible = false;

            this.btnStop.Enabled = false;
            this.btnStart.Enabled = true;
            this.pnMain.Enabled = true;
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialogPathToFiles.ShowDialog() != DialogResult.OK)
                return;

            string path = folderBrowserDialogPathToFiles.SelectedPath;

            if (!CheckIfDefaultAspxExistsInDirectory(path))
                return;

            this.txtPathToFiles.Text = path;
        }

        private static bool CheckIfDefaultAspxExistsInDirectory(string pathToDirectory)
        {
            //Check if Default.aspx exists
            if (!System.IO.File.Exists(pathToDirectory + "\\Default.aspx"))
            {
                MessageBox.Show("Default.aspx couldn't be found in the specified directory!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            return true;
        }

        private void linkLabelWebserverUrl_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(this.linkLabelWebserverUrl.Text);
        }
    }
}
